package org.example.registry;

import org.example.model.ServiceMetaInfo;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @description 本地缓存注册中心
 * @author sats@jz
 * @date 2025年01月03日 15:02
 */
public class RegistryServiceCache {

     Map<String, List<ServiceMetaInfo>> serviceCache = new ConcurrentHashMap<>();

    void writeCache(String key, List<ServiceMetaInfo> newServiceCache){
        serviceCache.put(key, newServiceCache);
    }

    List<ServiceMetaInfo> readCache(String key){
        return serviceCache.get(key);
    }

    Map<String, List<ServiceMetaInfo>> getAllCache(){
        return serviceCache;
    }

    void clearCache(String serviceKey, String serviceNodeKey){
        List<ServiceMetaInfo> serviceMetaInfos = serviceCache.get(serviceKey);
        serviceMetaInfos.removeIf(serviceMetaInfo -> serviceMetaInfo.getServiceNodeKey().equals(serviceNodeKey));
    }
}
